package xyz.yuanjilu.MPPlug.impl;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.riversoft.weixin.common.message.XmlMessageHeader;
import com.riversoft.weixin.common.message.xml.TextXmlMessage;
import com.riversoft.weixin.common.request.ImageRequest;
import com.riversoft.weixin.common.request.TextRequest;
import com.riversoft.weixin.common.request.VoiceRequest;

import xyz.yuanjilu.entity.CarEntity;
import xyz.yuanjilu.entity.CarWeixinSearchEntity;
import xyz.yuanjilu.entity.WeixinConfigEntity;
import xyz.yuanjilu.plug.MPPlug;
import xyz.yuanjilu.service.CarService;
import xyz.yuanjilu.service.CarWeixinSearchService;
import xyz.yuanjilu.service.OcrService;
import xyz.yuanjilu.utils.StringUtils;

@Component
public class CarMPPlugImpl implements MPPlug {
	@Autowired
	public CarService carService;
	@Autowired
	public CarWeixinSearchService carWeixinSearchService;
	@Autowired
	public OcrService ocrService;

	@Override
	public boolean superWeixinCode(WeixinConfigEntity weixinConfig) {
		return "c001".equals(weixinConfig.getWeixinCode());
	}

	@Override
	public String getTextMenu(WeixinConfigEntity weixinConfig) {
		return "回复查车+车牌即可查询，如：查车鄂AXXXXX,不区分大小写哦。\n也可以回复语音【查询车牌鄂AXXXXX】,\n或者发张车牌照片也可以试试";
	}

	@Override
	public String getDescribe(WeixinConfigEntity weixinConfig) {
		return "支持查询卡梅尔的车辆信息\n"+this.getTextMenu(weixinConfig);
	}


	@Override
	public String getName(WeixinConfigEntity weixinConfig) {
		return "卡梅尔车辆查询插件";
	}

	@Override
	public boolean superXmlMessageHeader(XmlMessageHeader xmlRequest, WeixinConfigEntity weixinConfig) {
		String keyWord="";
		if (xmlRequest instanceof TextRequest) {
			TextRequest request = (TextRequest) xmlRequest;
			keyWord=request.getContent();
			
			if (StringUtils.isNotBlank(keyWord)||keyWord.length()>2) {
				if (keyWord.indexOf("查车")>-1) {
					return true;
				}
			}
		}else if (xmlRequest instanceof VoiceRequest) {
			VoiceRequest request = (VoiceRequest) xmlRequest;
			keyWord=request.getRecognition();
			
			if (StringUtils.isNotBlank(keyWord)||keyWord.length()>2) {
				if (keyWord.indexOf("查询车牌")>-1) {
					return true;
				}
			}
		}else if (xmlRequest instanceof ImageRequest) {
			ImageRequest request = (ImageRequest) xmlRequest;
			String imgurl=request.getPicUrl();
			JSONObject ocrinfo = ocrService.plateLicenseURL(imgurl);
			keyWord=ocrinfo.getJSONObject("words_result").getString("number");
			if (keyWord.length()!=7) {
				return false;
			}else{
				return true;
			}
		}else{
			return false;
		}
		
		return false;
	}

	@Override
	public XmlMessageHeader handle(String weixinUserId, XmlMessageHeader xmlRequest, XmlMessageHeader xmlResponse,
			WeixinConfigEntity weixinConfig) {
		
		String keyWord="";
		String carPlateNumber="";
		if (xmlRequest instanceof TextRequest) {
			TextRequest request = (TextRequest) xmlRequest;
			keyWord=request.getContent();
			keyWord=StringUtils.exceptSymbol(keyWord);
			carPlateNumber=keyWord.toUpperCase().trim().substring(2);
		}
		if (xmlRequest instanceof VoiceRequest) {
			VoiceRequest request = (VoiceRequest) xmlRequest;
			keyWord=request.getRecognition();
			keyWord=StringUtils.exceptSymbol(keyWord);
			carPlateNumber=keyWord.toUpperCase().trim().substring(4);
		}
		if (xmlRequest instanceof ImageRequest) {
			ImageRequest request = (ImageRequest) xmlRequest;
			String imgurl=request.getPicUrl();
			JSONObject ocrinfo = ocrService.plateLicenseURL(imgurl);
			carPlateNumber=ocrinfo.getJSONObject("words_result").getString("number");
		}
		
		if (xmlResponse instanceof TextXmlMessage) {
			TextXmlMessage response = (TextXmlMessage) xmlResponse;
			String text="\n"+response.getContent();
			
			
			String carinfoFormat="今天还还可以查询{0}次\n车牌号为{1}\n车辆颜色为{2}\n所有者信息为{3}";
			if (carPlateNumber.length()!=7) {
				text="车牌"+carPlateNumber+"格式不正确,不是7位字符\n"+this.getTextMenu(weixinConfig);
				response.content(text);
				return response;
			}
			Map<String ,Object> param=new HashMap<String ,Object>();
			param.put("carPlateNumber", carPlateNumber);
			CarEntity queryCar = carService.queryCar(param);
			
			
			if (queryCar!=null) {
				int searchTotal = carWeixinSearchService.searchTotal(weixinUserId);
				if (searchTotal>2) {
					text="每天只可以查询3次哦";
				}else {
					int i = 3-searchTotal-1;
					text=MessageFormat.format(carinfoFormat,i, queryCar.getCarPlateNumber(),queryCar.getCarColor(),queryCar.getCarBelongToInfo());
					CarWeixinSearchEntity carWeixinSearchEntity=new CarWeixinSearchEntity();
					carWeixinSearchEntity.setWeixinUserId(weixinUserId);
					carWeixinSearchEntity.setWeixinId(weixinConfig.getId());
					carWeixinSearchEntity.setCarId(queryCar.getId());
					carWeixinSearchEntity.setInfo(keyWord+text);
					carWeixinSearchService.save(carWeixinSearchEntity);
				}
				
			}else{
				queryCar =new CarEntity();
				queryCar.setCarBelongToInfo("暂未完善,为了更好的方便其他业主联系你移车，回复该车牌的联系方式，我们在看到后会完善该车牌的所有者信息");
				queryCar.setCarPlateNumber(carPlateNumber);
				carService.save(queryCar);
				text="未查询到相关车辆信息，你提供的车牌号"+carPlateNumber+"已经记录等待完善";
			}
			
			response.setContent(text);
			return response;
		}else{
			return xmlResponse;
		}
		
	}
	
	
}
